﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for Customer
/// </summary>
public class Customer
{
    SqlConnection conn = Utils.getDBConnection();

    private string name = null;
    private string password = null;
    private string email = null;
    private string joindate = null;
    private string address = null;
    private string city = null;
    private string state = null;
    private string zip = null;


    public Customer()
    {
        //
        // TODO: Add constructor logic here


    }

        /// <summary>
    /// /List of all getters and setter for the customer class
    /// </summary>

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
  
    public string Email
    {
        get { return email; }
        set { email = value; }
    }
    public string Address
    {
        get { return address; }
        set { address = value; }
    }
    public string City
    {
        get { return city; }
        set { city = value; }
    }
    public string State
    {
        get { return state; }
        set { state = value; }
    }
    public string Joindate
    {
        get { return joindate; }
        set { joindate = value; }
    }
    public string Zip
    {
        get { return zip; }
        set { zip = value; }
    }
    public string Password
    {
        get { return password; }
        set { password = value; }
    }
    


    /// <summary>
    /// Method to get customer details given a customer emailid
    /// </summary>
    /// <param name="user">Customer email address</param>
    /// <returns>Customer object</returns>

    public Customer getCustomer(string user)
    {
        Customer cs = new Customer();

        //prepare SQL call
        SqlCommand storedProcCommand = new SqlCommand("dbo.mov_getCustomer", conn);
        storedProcCommand.CommandType = System.Data.CommandType.StoredProcedure;
        storedProcCommand.Parameters.AddWithValue("@email", user);        
        conn.Open();

        //exec SQL
        SqlDataReader reader = storedProcCommand.ExecuteReader();

        //populate customer object
        while (reader.Read())
        {
            cs.Email = reader["cus_email"].ToString();
            cs.Name = reader["cus_name"].ToString();
            cs.Password = reader["cus_password"].ToString();
            cs.Joindate = reader["cus_join_date"].ToString();
            cs.Address = reader["address1"].ToString();
            cs.City = reader["city"].ToString();
            cs.State = reader["state_long"].ToString();
            cs.Zip = reader["postalcode"].ToString();
            return cs;
        }

        //clean-up
        reader.Close();
        conn.Close();

        
        return null;
    }

    /// <summary>
    /// Save customer into the database
    /// </summary>
    /// <param name="cs">Customer object</param>
    /// <returns>true if customer is successfully created or false</returns>

    public bool saveCustomerInfo(Customer cs)
    {
        try
        {
            //prepare SQL call
            SqlCommand storedProcCommand = new SqlCommand("dbo.mov_insertCustomer", conn);
            storedProcCommand.CommandType = System.Data.CommandType.StoredProcedure;
            storedProcCommand.Parameters.AddWithValue("@email", cs.Email);
            storedProcCommand.Parameters.AddWithValue("@password", cs.Password);
            storedProcCommand.Parameters.AddWithValue("@name", cs.Name);
            storedProcCommand.Parameters.AddWithValue("@address", cs.Address);
            storedProcCommand.Parameters.AddWithValue("@city", cs.City);
            storedProcCommand.Parameters.AddWithValue("@zip", cs.Zip);
            storedProcCommand.Parameters.AddWithValue("@stateid", cs.State);
            
            conn.Open();

            //exec SQL
            int row = storedProcCommand.ExecuteNonQuery();

            conn.Close();
            return true;
        }
        catch (SqlException ex)
        {
            conn.Close();
            Console.WriteLine("Error inserting customer data:" + ex.Message);

        }
        return false;
    }

    /// <summary>
    /// Returns the rentals that are currently not returned by the customer
    /// </summary>
    /// <param name="email">Email id of the specific customer</param>
    /// <returns>Dataset object of all movies rented out</returns>
    public DataSet getRentals(string email)
    {
        DataSet ds = null;

        try
        {
        //prepare SQL call
        SqlCommand storedProcCommand = new SqlCommand("dbo.mov_getRentalsforCustomer", conn);
        storedProcCommand.CommandType = System.Data.CommandType.StoredProcedure;
        storedProcCommand.Parameters.AddWithValue("@email", email);
        conn.Open();

        //exec SQL
        SqlDataReader reader = storedProcCommand.ExecuteReader();
        
        ds = Utils.DataReaderToDataSet(reader);


        }
        catch (Exception ex)
        {

            ds = null;
            conn.Close();
            Console.WriteLine("Error inserting customer data:" + ex.Message);

        }

        conn.Close();
        return ds;

    }

    /// <summary>
    /// Method to check and see if a given email id already exists in the system
    /// </summary>
    /// <param name="email">customer email id</param>
    /// <returns>true if the customer exists or returns false</returns>
    public bool isExistingCustomer(string email)
    {
        Customer cs = this.getCustomer(email);
        if (cs != null)
            return true;
        else
            return false;
    }


}
